package cn.edu.xmu.restfuldemo.service;

import cn.edu.xmu.restfuldemo.model.BO.Log;
import cn.edu.xmu.restfuldemo.model.VO.OrdersRetVo;
import cn.edu.xmu.restfuldemo.util.JacksonUtil;
import cn.edu.xmu.restfuldemo.util.ResponseCode;
import cn.edu.xmu.restfuldemo.util.ReturnObject;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.time.LocalDateTime;


/**
 * 信息生产者
 * @author Ming Qiu
 * @date Created in 2020/11/7 22:53
 **/
@Service
public class RocketMQService {

    private static final Logger logger = LoggerFactory.getLogger(RocketMQService.class);

    @Resource
    private RocketMQTemplate rocketMQTemplate;


    @Value("${rocketmqdemo.order-pay-topic.delay-level}")
    private int delayLevel;

    @Value("${rocketmqdemo.order-pay-topic.timeout}")
    private long timeout;


    /**
     * 单向消息，采用sendOneWay方法，第一个参数topic:tag,第二个参数是消息
     * @param log
     */
    public void sendLogMessage(Log log){
        String json = JacksonUtil.toJson(log);
        Message message = MessageBuilder.withPayload(json).build();
        logger.info("sendLogMessage: message = " + message);
        rocketMQTemplate.sendOneWay("log-topic:1", message);
    }

    /**
     * 延时消息，采用asyncSend方法，第一个参数是topic,第二个参数是消息，第三个参数是回调函数sendCallback，第四个参数是间隔时间,第五个参数是延迟时间(从配置中获得)
     * @param orderId
     */
    public void sendOrderPayMessage(Long orderId){
        logger.info("sendOrderPayMessage: send message orderId = "+orderId+" delay ="+delayLevel+" time =" +LocalDateTime.now());
        rocketMQTemplate.asyncSend("order-pay-topic", MessageBuilder.withPayload(orderId.toString()).build(), new SendCallback() {

            /**
             * 回调函数中的成功回调
             * @param sendResult
             */
            @Override
            public void onSuccess(SendResult sendResult) {
                logger.info("sendOrderPayMessage: onSuccess result = "+ sendResult+" time ="+LocalDateTime.now());
            }

            /**
             * 回调函数中的异常回调
             * @param throwable
             */
            @Override
            public void onException(Throwable throwable) {
                logger.info("sendOrderPayMessage: onException e = "+ throwable.getMessage()+" time ="+LocalDateTime.now());
            }
        }, timeout * 1000, delayLevel);
    }

    /**
     * 异步消息
     * @param ordersRetVo
     */
    public void sendOrderMessage(OrdersRetVo ordersRetVo){

        String json = JacksonUtil.toJson(ordersRetVo);
        Message message = MessageBuilder.withPayload(json).build();
        rocketMQTemplate.asyncSend("orders-orderitem-topic", message, new SendCallback() {

            /**
             * 回调函数中的成功回调
             * @param sendResult
             */
            @Override
            public void onSuccess(SendResult sendResult) {
                logger.info("sendOrderMessage: onSuccess result = "+ sendResult+" time ="+LocalDateTime.now());
            }

            /**
             * 回调函数中的异常回调
             * @param throwable
             */
            @Override
            public void onException(Throwable throwable) {
                logger.info("sendOrderMessage: onException e = "+ throwable.getMessage()+" time ="+LocalDateTime.now());
            }
        }, timeout * 1000);
    }
}
